package com.dupengcheng.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

public class DruidEncryptUtil {

    private static  String publicKey;
    private static  String privateKey;




    static {


        try {
            String[] strings = ConfigTools.genKeyPair(512);
            publicKey=strings[1];
            System.out.println("publicKey:"+publicKey);
            privateKey=strings[0];
            System.out.println("privateKey:"+privateKey);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e);
        }


    }

    public static String encrypt(String plainText) throws Exception {
        String encrypt= ConfigTools.encrypt(privateKey, plainText);
        System.out.println("encrypt:"+encrypt);
        return encrypt;

    }
    public static String decrypt(String plainText) throws Exception {
        String decrypt= ConfigTools.decrypt(publicKey, plainText);
        System.out.println("decrypt:"+decrypt);
        return decrypt;

    }

    public static void main(String[] args) throws Exception {

        String encrypt = encrypt("QA1WS2aa@@");
        String url = encrypt("root");
        System.out.println("password:"+encrypt);
        System.out.println("url:"+url);

    }
}
